<template>
  <Popup
    v-model:show="showPopup"
    :style="{
      background: 'transparent',
      overflow: 'hidden',
      'max-width': '100vw',
    }"
  >
    <div class="vessel">
      <div class="heard-flex-box">
        <img
          class="top-button"
          v-for="(item, index) in tabImgList"
          :key="item?.src?.href"
          :src="item?.isActive ? item?.activeSrc?.href : item?.src?.href"
          @click="tabsClick(index)"
        />
      </div>
      <div class="phone-box">
        <img src="@/assets/image/phone-heard.png" class="phone-box-heard" />
        <div class="phone-body-box">
          <van-list
            v-if="friendList?.length > 0"
            v-model:loading="loading"
            :finished="finished"
            finished-text="没有更多了~"
            @load="
              () => {
                pagination.pageNum++;
                onLoad();
              }
            "
            style="
              display: flex;
              align-items: center;
              justify-content: center;
              gap: 10px;
              flex-direction: column;
            "
          >
            <van-cell
              style="padding: 0"
              v-for="item in friendList"
              :key="item"
              class="friend-box-item"
            >
              <div class="friend-box-item">
                <img :src="item?.avatar?.[0]?.fileUrl || avatar" />
                <div class="friend-middle-box">
                  <div class="friend-name">{{ item?.studentName }}</div>
                  <div class="friend-level">
                    {{ "Lv." + item?.grade + " &nbsp;" + item?.rankName }}
                  </div>
                </div>
                <div
                  v-if="tabValue == 0"
                  class="friend-button"
                  style="opacity: 0"
                ></div>
                <div v-if="tabValue == 1">
                  <div
                    class="friend-button"
                    @click="agreeFriendClick(item?.id)"
                  >
                    接受
                  </div>
                  <div
                    style="margin: 6px 0 0 0"
                    class="friend-button-one"
                    @click="disagreeFriendClick(item?.id)"
                  >
                    拒绝
                  </div>
                </div>
                <div
                  v-if="tabValue == 2"
                  :class="{
                    'friend-button': item?.agree == 1,
                    'friend-button-one': item?.agree != 1,
                  }"
                  :style="{ opacity: item?.agree == 1 ? 0 : 1 }"
                  @click="
                    item?.agree == 1 ||
                      postSendFriendApplyClick(item?.studentId)
                  "
                >
                  {{
                    item?.agree == 1
                      ? ""
                      : item?.agree == 0
                      ? "申请中"
                      : "添加好友"
                  }}
                </div>
              </div>
            </van-cell>
          </van-list>
          <div
            v-if="friendList?.length <= 0 && tabValue == 0"
            class="no-friend-box"
          >
            <span>你还没有任何好友喔，快去添加吧~</span>
            <div class="add-friend-button" @click="goAddFriendClick">添加</div>
          </div>
          <div
            v-if="friendList?.length <= 0 && tabValue == 1"
            class="no-friend-box"
          >
            <span>暂无好友申请</span>
          </div>
          <div
            v-if="friendList?.length <= 0 && tabValue == 2"
            class="no-friend-box"
          >
            <span>暂无同班同学</span>
          </div>
        </div>
        <img src="@/assets/image/phone-button.png" class="phone-box-button" />
      </div>
    </div>
  </Popup>
</template>

<script lang="ts" setup>
import {
  getClassmateList,
  getFriendApplyList,
  getUnreadMsg,
  postAgreeFriend,
  postRefuseFriend,
  postSendFriendApply,
  queryFriend,
} from "@/api/common";
import { useMainStore } from "@/store";
import { Popup } from "vant";
import { ref, watch } from "vue";
import avatar from "@/assets/svg/avatar.svg";
const showPopup = defineModel<boolean>("showPopup");
const tabValue = ref(0);
const friendList = ref([] as any);
const loading = ref(false);
const finished = ref(false);
const emit = defineEmits(["child-event"]);
const tabImgList = ref([
  {
    src: new URL("@/assets/image/friend/my-friend-grayed.png", import.meta.url),
    activeSrc: new URL("@/assets/image/friend/my-friend.png", import.meta.url),
    isActive: true,
  },
  {
    src: new URL(
      "@/assets/image/friend/friend-request-grayed.png",
      import.meta.url
    ),
    activeSrc: new URL(
      "@/assets/image/friend/friend-request.png",
      import.meta.url
    ),
    isActive: false,
  },
  {
    src: new URL(
      "@/assets/image/friend/classmates-grayed.png",
      import.meta.url
    ),
    activeSrc: new URL("@/assets/image/friend/classmates.png", import.meta.url),
    isActive: false,
  },
]);
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  searchCount: true,
  studentId: "",
});

function tabsClick(index: number) {
  tabImgList.value.forEach((item) => {
    item.isActive = false;
  });
  tabValue.value = index;
  finished.value = false;
  loading.value = false;
  tabImgList.value[index].isActive = true;
  pagination.value.pageNum = 1;
  friendList.value = [];
  onLoad();
}
const mainStore = useMainStore();
watch(
  () => showPopup.value,
  (newVal) => {
    if (newVal) {
      resetData();
      (pagination.value.studentId = mainStore.studentInfo?.studentId),
        getUnreadMsg(pagination.value.studentId).then((res) => {
          console.log(res);
        });
      onLoadFriend();
    }
  }
);

function resetData() {
  pagination.value.pageNum = 1;
  friendList.value = [];
}

function goAddFriendClick() {
  showPopup.value = false;
  emit("child-event");
}

function postSendFriendApplyClick(id: string) {
  postSendFriendApply({
    studentId: pagination.value.studentId,
    friendId: id,
  }).then(() => {
    friendList.value = [];
    pagination.value.pageNum = 1;
    finished.value = false;
    onLoad();
  });
}

function agreeFriendClick(id: string) {
  postAgreeFriend(id).then(() => {
    friendList.value = [];
    pagination.value.pageNum = 1;
    finished.value = false;
    onLoad();
  });
}

function disagreeFriendClick(id: string) {
  postRefuseFriend(id).then(() => {
    friendList.value = [];
    pagination.value.pageNum = 1;
    finished.value = false;
    onLoad();
  });
}

function onloadClassmate() {
  loading.value = true;
  getClassmateList(pagination.value).then((res) => {
    friendList.value = [...friendList.value, ...res.data.list];
    loading.value = false;
    if (!res.data.list.length) {
      finished.value = true;
    }
  });
}

function onLoadFriend() {
  loading.value = true;
  queryFriend(pagination.value).then((res) => {
    friendList.value = [...friendList.value, ...res.data.list];
    loading.value = false;
    if (!res.data.list.length) {
      finished.value = true;
    }
  });
}

function onLoadFriendApplyList() {
  loading.value = true;
  getFriendApplyList(pagination.value).then((res) => {
    friendList.value = [...friendList.value, ...res.data.list];
    loading.value = false;
    if (!res.data.list.length) {
      finished.value = true;
    }
  });
}

function onLoad() {
  switch (tabValue.value) {
    case 0:
      onLoadFriend();
      break;
    case 1:
      onLoadFriendApplyList();
      break;
    case 2:
      onloadClassmate();
      break;
  }
}
</script>

<style lang="scss" scoped>
.vessel {
  .heard-flex-box {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 13px;
    .top-button {
      width: 105px;
      height: 59px;
    }
  }
}
.phone-box {
  position: relative;
  width: 336px;
  height: 606px;
  background-image: url(@/assets/image/phone-background.png);
  background-size: 100% 100%;
  background-repeat: repeat;
  .no-friend-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 524px;
    gap: 17px;
    span {
      font-family: Source Han Sans;
      font-size: 14px;
      font-weight: 350;
      line-height: 22px;
      text-align: center;
      letter-spacing: normal;
      color: #333333;
    }
    .add-friend-button {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 120px;
      height: 40px;
      border-radius: 50px;
      /* 按钮黄色渐变 */
      background: linear-gradient(90deg, #ffc500 0%, #ffb200 100%);
      /* 特效 */
      box-shadow: inset -1px -1px 2px 0px #d89f37,
        inset 1px 1px 2px 0px rgba(255, 255, 255, 0.3);
      font-family: Source Han Sans;
      font-size: 14px;
      font-weight: 500;
      line-height: 22px;
      text-align: center;
      letter-spacing: normal;
      color: #ffffff;
    }
  }
  .phone-body-box {
    position: absolute;
    top: 28px;
    left: 15px;
    width: calc(306px - 20px);
    height: calc(524px - 20px);
    border-radius: 10px;
    background-color: #f6f6f6;
    display: flex;
    flex-direction: column;
    padding: 10px;
    gap: 10px;
    overflow: auto;
    .friend-box-item {
      width: 286px;
      height: 100px;
      border-radius: 10px;
      background: #ffffff;
      box-shadow: 0px 4px 10px 0px rgba(228, 228, 228, 0.3);
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 10px;
      .friend-button-one {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 74px;
        height: 34px;
        border-radius: 50px;
        border: 1px solid #ffc500;
        /* 正常/正文28 */
        font-family: Source Han Sans;
        font-size: 14px;
        font-weight: 350;
        line-height: 22px;
        text-align: center;
        letter-spacing: normal;
        /* 标准色 */
        color: #ffc500;
      }
      .friend-button {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 74px;
        height: 34px;
        border-radius: 50px;
        background: linear-gradient(90deg, #ffc500 0%, #ffb200 100%);
        box-shadow: inset -1px -1px 2px 0px #d89f37,
          inset 1px 1px 2px 0px rgba(255, 255, 255, 0.3);
        font-family: Source Han Sans;
        font-size: 14px;
        font-weight: 350;
        line-height: 22px;
        letter-spacing: normal;
        color: #ffffff;
      }
      .friend-middle-box {
        display: flex;
        flex-direction: column;
        .friend-name {
          font-family: Source Han Sans;
          font-size: 16px;
          font-weight: 350;
          line-height: 22.4px;
          letter-spacing: normal;
          /* 字体/333 */
          color: #333333;
          width: 100px;
          text-overflow: ellipsis;
          overflow: hidden;
          text-align: left;
          white-space: nowrap;
        }
        .friend-level {
          /* 正常/辅助24 */
          font-family: Source Han Sans;
          font-size: 12px;
          font-weight: 350;
          line-height: 20px;
          text-align: left;
          letter-spacing: normal;
          /* 标准色 */
          color: #ffc500;
        }
      }
      img {
        border-radius: 80px;
        width: 60px;
        height: 60px;
      }
    }
  }
  .phone-box-heard {
    position: absolute;
    top: 7px;
    width: 87px;
    height: 16px;
    left: 124px;
  }
  .phone-box-button {
    width: 47.5px;
    height: 47.5px;
    position: absolute;
    bottom: 5px;
    left: 138.51px;
  }
}
</style>
